package com.llp.declaresystem.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llp.declaresystem.domain.dto.UserDto;
import com.llp.declaresystem.domain.entity.Expert;
import com.llp.declaresystem.domain.entity.Project;
import com.llp.declaresystem.domain.entity.ProjectCategory;
import com.llp.declaresystem.domain.entity.User;
import com.llp.declaresystem.mapper.ProjectCategoryMapper;
import com.llp.declaresystem.mapper.ProjectMapper;
import com.llp.declaresystem.mapper.UserMapper;
import com.llp.declaresystem.service.ExpertService;
import com.llp.declaresystem.mapper.ExpertMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author NASK
 * @description 针对表【expert(专家信息表)】的数据库操作Service实现
 * @createDate 2022-11-23 16:22:17
 */
@Service
public class ExpertServiceImpl extends ServiceImpl<ExpertMapper, Expert>
        implements ExpertService {

    @Autowired
    private ExpertMapper expertMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectCategoryMapper projectCategoryMapper;
    /**
     * 根据projceid查询项目信息
     * @param pid
     * @return
     */
    @Override
    public Project selectProjectByPid(Integer pid) {
        Project project = projectMapper.selectByPid(pid);
        return project;
    }

    /**
     * 根据username修改用户信息
     *
     * @param expert
     * @return
     */
    @Override
    public int updateByName(Expert expert) {
        return expertMapper.updateInfo(expert);
    }

    /**
     * 根据uid修改用户密码
     * @param userDto
     * @return
     */
    @Override
    public int updatePwdByUid(UserDto userDto) {
        User user = userMapper.selectByUid(userDto.getUid());
        System.out.println("user:"+user);
        if (user != null) {
            if (StringUtils.equals(user.getPassword(), userDto.getOldPassword())) {
                int res = userMapper.updatePwdByUid(userDto);
                System.out.println("res:"+res);
                return res;
            } else {
                return -1;
            }
        }
        return 0;
    }

    /**
     * 返回项目类别信息
     * @param uid
     * @return
     */
    @Override
    public List<ProjectCategory> selectAllProjectCategory(Integer uid) {
        Expert expert = expertMapper.selectByUid(uid);
        if(expert!=null){
            Integer expertId = expert.getExpertid();
            List<ProjectCategory> projectCategory = projectCategoryMapper.searchAllByExpertId(expertId);
            return projectCategory;
        }else{
            return null;
        }
    }

    /**
     * 更新项目信息
     * @param project
     * @return
     */
    @Override
    public int updateProjectInfo(Project project) {
        Project project1 = projectMapper.selectAllByProjectId(project.getProjectId());
        if(project1!=null){
            int res = projectMapper.updateProjectByProjectId(project);
            return res;
        }
        return -1;
    }

    /**
     * 根据expertId去查找项目信息
     * @param categoryId
     * @return
     */
    @Override
    public List<Project> selectProjectByCid(Integer categoryId) {
        List<Project> project = projectMapper.selectProjectByCategoryId(categoryId);
        return project;
    }

}




